/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testalgorithm;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author haoliang.donghl
 */
public class GenerateForecast {
    static float[] numArray;
    static float[] initArray;
    static float WAVE_COFF = 0.2f;
    static int REPEAT_TIME = 3;
    static float[] WEIGHTS = {1.0f, 1.1f, 1.0f};
    static float WEIGHT = 0.3f;
    static float WEIGHT_COFF = 0.4f;
    static BufferedWriter bw;
    static Random random = new Random();

    private static void readFile(String filename){
        try {
            Scanner scanner = new Scanner(new File(filename));
            String fileContent = scanner.nextLine();
            String[] strArray = fileContent.split(",");
            numArray = new float[strArray.length];
            for (int i = 0; i < numArray.length; i++){
                numArray[i] = Float.parseFloat(strArray[i]);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            System.out.println(e);
        }
    }

    private static void randomNumber(int index) {
        float weight;
        if (WEIGHTS == null){
            weight = WEIGHT;
        }else{
            if (index < WEIGHTS.length){
                weight = WEIGHTS[index];
            }else{
                weight = WEIGHT;
            }
        }
        for (int i = 0; i < numArray.length; i++){
            numArray[i] = (initArray[i] +
                          ((float)random.nextGaussian())
                          * WAVE_COFF
                          * initArray[i]) * weight;
            if (numArray[i] < 0) numArray[i] = 0;
        }
    }


    private static void printResult() {
        if (numArray.length != 0){
            System.out.print((int)numArray[0]);
        }
        for (int i = 1; i < numArray.length; i++){
            System.out.print(",");
            System.out.print((int)numArray[i]);
        }
    }

    private static void printInitArray(){
        try{
         
            if (initArray.length != 0){
                bw.write(String.valueOf((int)initArray[0]));
            }
            for (int i = 1; i < initArray.length; i++){
                bw.write(",");
                bw.write(String.valueOf((int)initArray[i]));
            }
        }catch(IOException e){
            System.out.println(e);
        }
    }

    private static void outputToFile(){
        try{
            if (numArray.length != 0){
                bw.write(String.valueOf((int)numArray[0]));
            }
            for (int i = 1; i < numArray.length; i++){
                bw.write("\r\n");
                bw.write(String.valueOf((int)numArray[i]));
            }
        }catch(IOException e){
            System.out.println(e);
        }

/*
        if (numArray.length != 0){
            System.out.print((int)numArray[0]);
        }
        for (int i = 1; i < numArray.length; i++){
            System.out.print(",");
            System.out.print((int)numArray[i]);
        }
 */
    }

    private static void copyArray() {
        initArray = new float[numArray.length];
        for (int i = 0; i < numArray.length; i++){
            initArray[i] = numArray[i];
        }
    }

    public static void main(String[] args){
        for (int i = 0; i < WEIGHTS.length ;i++){
            WEIGHTS[i] = (float) (1 + random.nextGaussian() * WEIGHT_COFF);
        }
        WEIGHTS = null;
        readFile("forecast.txt");
        try {
            bw = new BufferedWriter(new FileWriter("output.txt"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        copyArray();
        //printInitArray();
        for (int i = 0; i < REPEAT_TIME; i++){
            try {
                randomNumber(i);
                outputToFile();
                bw.write("\r\n");
            } catch (IOException ex) {
                Logger.getLogger(GenerateForecast.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            bw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}
